<template>
  <div class="modleCls">
    <div :style="{height: toolbarHei +'px'}"></div>
    <nut-navbar title="终端编辑" left-text="返回" left-show @click-back="clickLeft"></nut-navbar>
    <div style="height: 10px;"></div>
    <div style="margin: 0 10px;background-color: white;padding:10px;border-radius:4px;">
      <div class="tc mb10" style="font-size: 18px;color: #1e9fff;font-weight: bold;">{{title}}</div>
      <nut-form :model-value="formData">
        <nut-form-item label="分类">
          <JPop cat="contactCat" v-model:id="formData.contactCatId" v-model="formData.contactCatName"></JPop>
        </nut-form-item>
        <nut-form-item label="业务员">
           <JAuto cat="admin" v-model:id="formData.salesId" v-model="formData.salesName"></JAuto>
        </nut-form-item>
        <nut-form-item label="名称">
          <nut-input v-model="formData.name"></nut-input>
        </nut-form-item>
        <nut-form-item label="手机">
          <nut-input v-model="formData.linkMobile"></nut-input>
        </nut-form-item>
        <nut-form-item label="联系人">
          <nut-input v-model="formData.linkName"></nut-input>
        </nut-form-item>
        <nut-form-item label="座机">
          <nut-input v-model="formData.linkPhone"></nut-input>
        </nut-form-item>
        <nut-form-item label="地址">
          <nut-input v-model="formData.address"></nut-input>
        </nut-form-item>
      </nut-form>
      <div style="margin-top:30px;">
<!--        <photo @suc="uploadSuc" />-->
      </div>
      <div class="tc mt20 pb10">
        <nut-button  @click="clickLeft">返回</nut-button>
        <nut-button style="width: 80px;margin-left: 50px;" type="info" @click="save" :loading="state.loading">保存</nut-button>
      </div>
    </div>
  </div>

</template>
<script setup>
// 增加客户
import tool from "@/utils/tool";
import {useGlobal} from "@/stores/useGlobal";
import bus from "@/utils/bus";
import {computed, onActivated, ref} from "vue";
import JPop from "@/components/JPop.vue";
import JAuto from "@/components/JAuto.vue";
import {useRoute, useRouter} from "vue-router";
const global = useGlobal()
const route = useRoute()
const router = useRouter()
const props = defineProps({
  title: {type: String,default: ''}
})
const emit = defineEmits(['saved'])
const toolbarHei = computed(() => {
  return global.statusBarHeight
})
const formData = ref({
  id: 0,
  imageUrl: '',
  contactCatName: '',
  contactCatId: null,
  name: '',
  linkName: '',
  linkMobile: '',
  linkPhone: '',
  address: '',
  isCustomer: 1,
  salesName: '',
  salesId: '',
})
const state = ref({
  formData1:{
    id: 0,
    imageUrl: '',
    contactCatId: null,
    name: '',
    linkName: '',
    linkMobile: '',
    linkPhone: '',
    address: '',
    isCustomer: 1,
    salesId: '',
  },
  cats: [],
  showForm: false,
  loading: false,
})
onActivated(() => {
  if(route.query && route.query.good) {
    let good = JSON.parse(route.query.good)
    for(let i in formData.value) {
      formData.value[i] = good[i]
    }
  }
  else {
    formData.value = Object.assign({}, state.formData1)
  }
})

const uploadSuc = (fileName) => {
  formData.value.imageUrl = fileName
}
const clickLeft = () => {
  router.go(-1)
}
const setData = (data) => {
  if(data.id) formData.value.id = data.id
  if(data.contactCatId) formData.value.contactCatId = tool.xe.toNumber(data.contactCatId)
  if(data.name) formData.value.name = data.name
  if(data.address) formData.value.address = data.address
  if(data.linkName) formData.value.linkName = data.linkName
  if(data.linkMobile) formData.value.linkMobile = data.linkMobile
  if(data.linkPhone) formData.value.linkPhone = data.linkPhone
  if(data.imageUrl) formData.value.imageUrl = data.imageUrl
}
const save = () => {
  if(!formData.value.name) return tool.err('请输入名称')
  if(!formData.value.linkName) return tool.err('请输入联系人')
  if(!formData.value.address) return tool.err('请输入地址')
  state.loading = true
  setTimeout(() => {
    state.loading = false
  }, 1000)
  if(!formData.value.id) {
    let usr = tool.data.getUser()
    formData.value.salesId = usr.id
  }
  tool.post('contact/save', {formData: formData.value}).then(ret => {
    state.loading = false
    if(ret.stat) {
      router.go(-1)
      bus.$emit('contactSaveSuc')
    }
  })
}

</script>
<style scoped lang="scss">
.modleCls {

}
</style>


